package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AssetsMapper;
import com.ruoyi.system.domain.Assets;
import com.ruoyi.system.service.IAssetsService;

/**
 * 管辖资产Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-17
 */
@Service
public class AssetsServiceImpl implements IAssetsService {
    @Autowired
    private AssetsMapper assetsMapper;

    /**
     * 查询管辖资产
     *
     * @param assetsId 管辖资产主键
     * @return 管辖资产
     */
    @Override
    public Assets selectAssetsByAssetsId(Long assetsId) {
        return assetsMapper.selectAssetsByAssetsId(assetsId);
    }

    /**
     * 查询管辖资产列表
     *
     * @param assets 管辖资产
     * @return 管辖资产
     */
    @Override
    public List<Assets> selectAssetsList(Assets assets) {
        return assetsMapper.selectAssetsList(assets);
    }

    /**
     * 新增管辖资产
     *
     * @param assets 管辖资产
     * @return 结果
     */
    @Override
    public int insertAssets(Assets assets) {
        return assetsMapper.insertAssets(assets);
    }

    /**
     * 修改管辖资产
     *
     * @param assets 管辖资产
     * @return 结果
     */
    @Override
    public int updateAssets(Assets assets) {
        return assetsMapper.updateAssets(assets);
    }

    /**
     * 批量删除管辖资产
     *
     * @param assetsIds 需要删除的管辖资产主键
     * @return 结果
     */
    @Override
    public int deleteAssetsByAssetsIds(Long[] assetsIds) {
        return assetsMapper.deleteAssetsByAssetsIds(assetsIds);
    }

    /**
     * 删除管辖资产信息
     *
     * @param assetsId 管辖资产主键
     * @return 结果
     */
    @Override
    public int deleteAssetsByAssetsId(Long assetsId) {
        return assetsMapper.deleteAssetsByAssetsId(assetsId);
    }

    /**
     * 批量导入管辖资产
     *
     * @param assetsList Excel文件
     * @return 导入结果数量
     */
    @Override
    public AjaxResult importAssets(List<Assets> assetsList) {
////            // 解析Excel文件，需要实现ExcelUtils工具类
//            List<Assets> assetsList = ExcelUtils.readExcel(file.getInputStream(), Assets.class);

        List<Assets> addAssetsList = new ArrayList<>();
        AtomicInteger addCount = new AtomicInteger();
        AtomicInteger updateCount = new AtomicInteger();
        assetsList.forEach(assets -> {
            boolean exists = assetsMapper.checkExistsByAssetNumber(assets);
            if (exists) {
                assets.setUpdateTime(new Date());
                assetsMapper.updateAssetsByAssetNumber(assets);
                updateCount.getAndIncrement();
            } else {
                addAssetsList.add(assets);
            }
        });
        // 批量插入，需要在AssetsMapper中添加batchInsertAssets方法
        if (!addAssetsList.isEmpty()) {
            int insertCount = assetsMapper.batchInsertAssets(addAssetsList);
            addCount.addAndGet(insertCount);
        }

        return AjaxResult.success("导入成功，共新增" + addCount.get() + "条数据，修改" + updateCount.get() + "条数据");
    }

}
